<?php

namespace addons\TfFutures\backend\controllers;

use addons\TfFutures\common\models\ArticleCate;
use addons\TfFutures\common\models\SymbolCate;
use addons\TfFutures\common\models\Symbol;
use addons\TfFutures\common\models\Lever;

use yii;
use common\components\Curd;
use yii\data\ActiveDataProvider;
use common\helpers\ArrayHelper;

class SymbolCateController extends BaseController
{
    use Curd;
    public $modelClass = SymbolCate::class;

    public function actionIndex()
    {
        $search = Yii::$app->request->get('keyword','');
        $query = SymbolCate::find()
            ->orderBy('sort asc, created_at asc')
            ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);
        $query->andFilterWhere(['like','title',$search]);
        //echo '<pre>';
        //print_r($query->asArray()->all());die;
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => false
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'keyword'      => $search,
        ]);
    }

    /**
     * @param $id 分类id
     */
    /*public function actionEditTn($id){
        //查询该分类
        $cateModel = SymbolCate::findOne($id);
        // print_r($cateModel);die;
        if ($cateModel->load(Yii::$app->request->post())) {

            //先查询该分类的子分类
            $childCate = SymbolCate::find()->select('id')->where(['pid'=>$id,'status'=>1])->asArray()->all();
            $childCateId = ArrayHelper::getColumn($childCate,'id');
            //更新所有产品的T+N为指定的数
            Symbol::updateAll(['trade_day'=>$cateModel->tn],['in','cate_id',$childCateId]);

            if ($cateModel->save()) {
                $this->message("修改成功", $this->redirect(['index']), 'success');
            } else {
                $this->message($this->getError($cateModel), $this->redirect(['index']), 'error');
            }

        }

        return $this->renderAjax('edit-tn', ['model' => $cateModel]);
    }*/
    /**
     * @param $id 分类id
     */
    /*public function actionEditTime($id){
        //查询该分类
        $cateModel = SymbolCate::findOne($id);
        // print_r($cateModel);die;
        if ($cateModel->load(Yii::$app->request->post())) {
            //先查询该分类的子分类
            $childCate = SymbolCate::find()->select('id')->where(['pid'=>$id,'status'=>1])->asArray()->all();
            $childCateId = ArrayHelper::getColumn($childCate,'id');
            //更新所有产品的交易时间
            SymbolCate::updateAll(['time' => $cateModel->time], ['in', 'id', $childCateId]);
            Symbol::updateAll(['trade_time'=>$cateModel->time],['in','cate_id',$childCateId]);

            if ($cateModel->save()) {
                $this->message("修改成功", $this->redirect(['index']), 'success');
            } else {
                $this->message($this->getError($cateModel), $this->redirect(['index']), 'error');
            }

        }
        //echo '<pre>';
        //print_r($cateModel);die;
        return $this->renderAjax('edit-time', ['model' => $cateModel]);
    }*/
    /**
     * @param $id 分类id
     */
    public function actionEditAll($id){
        //查询该分类
        $cateModel = SymbolCate::findOne($id);
        //$lever = Lever::find()->select('id,name')->asArray()->all();

        $name = Yii::$app->request->post();

        if (!empty($name)) {
            $name = array_filter($name);
            unset($name['_csrf-backend']);
            $name['trade_day']= Yii::$app->request->post('trade_day');
            if(empty($name['trade_day'])){
                unset($name['trade_day']);
            }

            //先查询该分类的子分类
            $childCate = SymbolCate::find()->select('id')->where(['pid'=>$id,'status'=>1])->asArray()->all();
            if(empty($childCate)){
                $childCate = SymbolCate::find()->select('id')->where(['id'=>$id,'status'=>1])->asArray()->all();
            }
            $childCateId = ArrayHelper::getColumn($childCate,'id');
            foreach ($name as $k => $val){
                //更新所有产品
                $save = Symbol::updateAll([$k=>$val],['in','cate_id',$childCateId]);
            }
            $this->message("修改成功", $this->redirect(['index']), 'success');
        }
        return $this->renderAjax('edit-all', ['model' => $cateModel]);
    }
    /**
     * 更新分类买卖方向并且同时更新分类下的所有产品的买卖方向
     * @param $id 分类id
     */
    public function actionEditRisk($id)
    {
        //查询该分类
        $cateModel = SymbolCate::findOne($id);
        if ($cateModel->load(Yii::$app->request->post())) {

            //先查询该分类的子分类
            $childCate = SymbolCate::find()->select('id')->where(['pid' => $id, 'status' => 1])->asArray()->all();
            $childCateId = ArrayHelper::getColumn($childCate, 'id');

            //更新所有产品买卖方向
            SymbolCate::updateAll(['rise_fall' => $cateModel->rise_fall], ['in', 'id', $childCateId]);

            Symbol::updateAll(['rise_fall' => $cateModel->rise_fall], ['in', 'cate_id', $childCateId]);

            if ($cateModel->save()) {
                $this->message("修改成功", $this->redirect(['index']), 'success');
            } else {
                $this->message($this->getError($cateModel), $this->redirect(['index']), 'error');
            }
        }

        return $this->renderAjax('edit-risk', ['model' => $cateModel]);
    }

    /**
     * @return mixed|string|\yii\console\Response|\yii\web\Response
     * @throws \yii\base\ExitException
     */
    public function actionAjaxEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id');
        $model = $this->findModel($id);
        $model->pid = $request->get('pid', null) ?? $model->pid; // 父id

        // ajax 验证
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
            return $model->save()
                ? $this->redirect(['index'])
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
        }

        return $this->renderAjax($this->action->id, [
            'model' => $model,
            'cateDropDownList' => SymbolCate::getEditDropDownList($id),
        ]);
    }
    /**
     * 删除
     *
     * @param $id
     * @return mixed
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete($id)
    {
        $symbolcate = SymbolCate::find()->where(['pid' => $id])->asArray()->all();
        if(!empty($symbolcate)){
            foreach ($symbolcate as $k=>$val){
                $symbol = Symbol::find()->where(['cate_id' => $val['id']])->asArray()->all();
                foreach ($symbol as $key=>$value){
                    Symbol::findOne($value['id'])->delete();
                }
                SymbolCate::findOne($val['id'])->delete();
            }
        }else{
            $symbol = Symbol::find()->where(['cate_id' => $id])->asArray()->all();
            foreach ($symbol as $key=>$value){
                Symbol::findOne($value['id'])->delete();
            }
        }
        if ($this->modelClass::findOne($id)->delete()) {
            return $this->message("删除成功", $this->redirect(['index']));
        }

        return $this->message("删除失败", $this->redirect(['index']), 'error');
    }

}